package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.AirLane;
import com.example.entity.OperationRecord;
import com.example.mapper.OperationRecordMapper;
import com.example.service.interfaces.OperationRecordInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class OperationRecordService extends ServiceImpl<OperationRecordMapper, OperationRecord> implements OperationRecordInterface {

    @Autowired
    private OperationRecordMapper operationRecordMapper;

    public OperationRecord isave(OperationRecord user){
        operationRecordMapper.insert(user);
        return user;
    }


    public Optional<OperationRecord> updatenew(OperationRecord user) {
        OperationRecord record = operationRecordMapper.selectById(user.getOperationId());
        if (user.getStartStopState()!=null){
            record.setStartStopState(user.getStartStopState());
        }
        if (user.getCorrespondingTimePeriod()!=null){
            record.setCorrespondingTimePeriod(user.getCorrespondingTimePeriod());
        }
        if (user.getCapacityThreshold()!=null){
            record.setCapacityThreshold(user.getCapacityThreshold());
        }
        if (user.getSafeIntervalThreshold()!=null){
            record.setSafeIntervalThreshold(user.getSafeIntervalThreshold());
        }
        int i = operationRecordMapper.updateById(record);
        if (i == 1) {
            return Optional.of(record);
        }
        return Optional.empty();
    }

    public boolean delete(OperationRecord user) {
        if (operationRecordMapper.selectById(user.getOperationId()) != null) {
            operationRecordMapper.deleteById(user.getOperationId());
            return true;
        }
        return false;
    }

    public List<OperationRecord> findByIdAndAirspaceObjectName(Integer objectId, String airspaceObjectName) {
        QueryWrapper<OperationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("object_id", objectId)
                .eq("airspace_object_name", airspaceObjectName);
        return operationRecordMapper.selectList(queryWrapper);
    }
}
